//
//  BaseViewController.swift
//  weibo
//
//  Created by hm on 2020/4/21.
//  Copyright © 2020 hm. All rights reserved.
//

import UIKit


class BaseViewController: UIViewController {
    // MARK:- 懒加载属性
    lazy var visitorView: VisitorView = VisitorView.instance()
    
    // MARK:- 定义变量
    var isLogin: Bool = true
    
    // MARK:- 系统回调函数
    override func loadView() {
        isLogin ? super.loadView() : setupVisitorView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupNavigationItems()
    }

}

// MARK:- 设置UI
extension BaseViewController {
    //显示访客视图
    private func setupVisitorView(){
        view = visitorView
        
        //注册访客视图中的注册、登陆按钮点击事件
        visitorView.registerBtn.addTarget(self, action: #selector(self.registerBtnClick), for: .touchUpInside)
        visitorView.loginBtn.addTarget(self, action: #selector(self.loginBtnClick), for: .touchUpInside)
    }
    
    //设置导航栏左右按钮
    private func setupNavigationItems(){
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(self.registerBtnClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: .plain, target: self, action: #selector(self.loginBtnClick))
    }
}


// MARK:- 事件监听
extension BaseViewController {
    @objc private func registerBtnClick(){
        print("register")
    }
    
    @objc private func loginBtnClick(){
        //创建授权View
        let oauthVc = OAuthViewController()
        
        //包装导航栏控制器
        let oauthNav = UINavigationController(rootViewController: oauthVc)
        //全屏显示
        oauthNav.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        //弹出控制器
        present(oauthNav, animated: true, completion: nil)
    }
}
